package com.eims.sls_app.ui;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.PopupWindow;

import bls.juejin.cn.R;
import com.eims.sls_app.ui.fragment_type.JxccsBaseFragment;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.LotteryHelp;
import com.eims.sls_app.view.MyToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 江西时时彩 的选球页面 功能： 选号界面，实现选号
 */
public class Select_JXSSCActivity extends BaseSelectActivity implements
		JxccsBaseFragment.OnFragmentInteractionListener {
	private final static String TAG = "Select_SSCActivity";
	private int containerID;
	private JxccsBaseFragment fragment;
	private int count;
	private Button btn_playHelp, btn_winNumber, btn_forgetNum;// 玩法说明，开奖详情，显示遗漏值
	private PopupWindow popWindow;

	// 这里6103为所有一到五星的直选，为了加以区分这里后面加上1-5，在投注的时候消除即可
	// private final int[] typeIDs = {6113, 6114, 6115, 6104, 61031
	// , 61032, 6106, 6110, 6133, 61033
	// , 6111, 6112, 6119, 6117, 6118
	// , 6116, 61034, 6132, 6131, 6130
	// , 6129, 61035, 6105, 6121, 6120
	// , 6126, 6125, 6124, 6123, 6127
	// , 6128, 6122};
	// private final String[] typeNames = {"任选一", "任选二", "任选三", "大小单双", "一星"
	// , "二星直选", "二星组选", "二星和值", "趣味二星", "三星直选"
	// , "三星组三", "三星组六", "三组包胆", "三星直选和值", "三星组选和值"
	// , "三星组合", "四星直选", "四星组选4", "四星组选6", "四星组选12"
	// , "四星组选24", "五星直选", "五星通选", "五星组选5", "五星组选10"
	// , "五星组选20", "五星组选30", "五星组选60", "五星组选120", "五星好事成双"
	// , "五星三星报喜", "五星四季发财"};

	// 这里6103为所有一到五星的直选，为了加以区分这里后面加上1-5，在投注的时候消除即可 清楚了暂停的玩法！！！！！
	private final int[] typeIDs = { 6113, 6114, 6115, 6104, 61031, 61032, 6106, /*
																				 * 6110
																				 * ,
																				 */
			6133, 61033, 6111, 6112, /* 6119, 6117, 6118 */
			/* , 6116, */61034, 6132, 6131, 6130, 6129, 61035, 6105, 6121,
			6120, 6126, 6125, 6124, 6123, 6127, 6128, 6122 };
	private final String[] typeNames = { "任选一", "任选二", "任选三", "大小单双", "一星",
			"二星直选", "二星组选", /* "二星和值", */"趣味二星", "三星直选", "三星组三", "三星组六", /*
																		 * "三组包胆",
																		 * "三星直选和值"
																		 * ,
																		 * "三星组选和值"
																		 */
			/* , "三星组合", */"四星直选", "四星组选4", "四星组选6", "四星组选12", "四星组选24",
			"五星直选", "五星通选", "五星组选5", "五星组选10", "五星组选20", "五星组选30", "五星组选60",
			"五星组选120", "五星好事成双", "五星三星报喜", "五星四季发财" };

	@Override
	protected void initFrameLayout(FrameLayout container, int containerID) {
		this.containerID = containerID;

		setTypeName(typeNames[0]);

		String winNum = App.lottery == null ? null : App.lottery
				.getLastWinNumber();

		setNameAndNum();
		Bundle bundle = getIntent().getBundleExtra("bundle");
		if (bundle != null) {
			backNewIntent(getIntent());
		} else {
			setFragmentItem(JxccsBaseFragment.newInstance(typeIDs[0],
					typeNames[0], 0));
		}
	}

	@Override
	protected Map<Integer, Map<Integer, String>> convertPopData() {
		Map<Integer, Map<Integer, String>> mapMap = new HashMap<Integer, Map<Integer, String>>();
		Map<Integer, String> map = new HashMap<Integer, String>();
		int k = 0;
		for (int i = 0; i < typeNames.length; i++) {
			// 判断条件为屏蔽的玩法
			// if (!LotteryHelp.containHideType(typeIDs[i]+"")){
			map.put(k++, typeNames[i]);
			// Log.e("zxcvb",typeNames[i]);
			// }
		}
		mapMap.put(0, map);
		return mapMap;
	}

	@Override
	protected void changePlayType(int parent, int child) {
		setFragmentItem(JxccsBaseFragment.newInstance(typeIDs[child],
				typeNames[child], child));
	}

	@Override
	protected void sensorChanged() {
		if (fragment != null) {
			fragment.onShakeClick();
		}
	}

	@Override
	protected boolean hasSelected() {
		return fragment != null && fragment.hasSelected();
	}

	@Override
	protected void backNewIntent(Intent intent) {
		Bundle bundle = intent.getBundleExtra("bundle");
		if (bundle == null)
			return;
		String TypeName = bundle.getString("playTypeName");
		int TypeId = bundle.getInt("playType");
		String position = bundle.getString("position");
		setPosition(position == null ? -1 : Integer.valueOf(position));
		List<String> one = bundle.getStringArrayList("one");
		List<String> two = bundle.getStringArrayList("two");
		List<String> three = bundle.getStringArrayList("three");
		List<String> four = bundle.getStringArrayList("four");
		List<String> five = bundle.getStringArrayList("five");
		JxccsBaseFragment jxccsBaseFragment = JxccsBaseFragment.newInstance(
				TypeId, TypeName, getIndexByTypeId(TypeId));
		jxccsBaseFragment.setAdapterData(five, four, three, two, one);
		setFragmentItem(jxccsBaseFragment);
		setTypeName(typeNames[mItemIndex = getIndexByTypeId(TypeId)]);
	}

	// protected void btn_help() {
	// Intent intent = new Intent(this, PlayDescription.class);
	// startActivity(intent);
	// }
	//
	protected void btn_clearall() {
		if (fragment != null) {
			fragment.clear();
		}
		count = 0;
		setCountAndMoney(count);
	}

	// 显示玩法说明
	@Override
	public void btn_help(View view) {
		createPopWindow();
		popWindow.showAsDropDown(view);
		// Intent intent = new Intent(this, PlayDescription.class);
		// startActivity(intent);
	}

	/**
	 * 玩法说明
	 */
	private void playExplain() {
		Intent intent = new Intent(this, PlayDescription.class);
		startActivity(intent);
	}

	/**
	 * 开奖详情
	 */
	private void winDetail() {
		Intent intent = new Intent(this, WinLotteryInfoActivity.class);
		intent.putExtra("lotteryId", App.lottery.getLotteryID());
		startActivity(intent);
	}

	private void createPopWindow() {
		LayoutInflater inflact = LayoutInflater.from(Select_JXSSCActivity.this);
		View view = inflact.inflate(R.layout.pop_item, null);
		btn_playHelp = (Button) view.findViewById(R.id.btn_playhelps);
		btn_winNumber = (Button) view.findViewById(R.id.btn_win);
		btn_forgetNum = (Button) view.findViewById(R.id.btn_omissive);// 显示遗漏值
		popWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		popWindow.setFocusable(true); // 设置PopupWindow可获得焦点
		popWindow.setTouchable(true); // 设置PopupWindow可触摸
		popWindow.setOutsideTouchable(true); // 设置PopupWindow外部区域是否可触摸
		// 设置之后点击返回键 popwindow 会消失
		popWindow.setBackgroundDrawable(new BitmapDrawable());
		popWindow.setFocusable(true);
		popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
			@Override
			public void onDismiss() {
			}
		});
		// 设置popwindow的消失事件
		// 监听返回按钮事件，因为此时焦点在popupwindow上，如果不监听，返回按钮没有效果
		OnKeyListener listener = new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				switch (keyCode) {
				case KeyEvent.KEYCODE_BACK:
					if (popWindow != null && popWindow.isShowing()) {
						popWindow.dismiss();
					}
					break;
				}
				return true;
			}
		};

		btn_playHelp.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				playExplain();
				popWindow.dismiss();
			}
		});
		btn_winNumber.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				winDetail();
				popWindow.dismiss();
			}
		});
		btn_forgetNum.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (JxccsBaseFragment.isvisible) {
					JxccsBaseFragment.isvisible = false;
					JxccsBaseFragment.mAdapterOne.setVisibles(false);
					JxccsBaseFragment.mAdapterTwo.setVisibles(false);
					JxccsBaseFragment.mAdapterThree.setVisibles(false);
					JxccsBaseFragment.mAdapterFour.setVisibles(false);
					JxccsBaseFragment.mAdapterFive.setVisibles(false);
					JxccsBaseFragment.mAdapterOne.notifyDataSetChanged();
					JxccsBaseFragment.mAdapterTwo.notifyDataSetChanged();
					JxccsBaseFragment.mAdapterThree.notifyDataSetChanged();
					JxccsBaseFragment.mAdapterFour.notifyDataSetChanged();
					JxccsBaseFragment.mAdapterFive.notifyDataSetChanged();
					return;
				}
				if (!JxccsBaseFragment.isvisible) {
					JxccsBaseFragment.isvisible = true;
					JxccsBaseFragment.mAdapterOne.setVisibles(true);
					JxccsBaseFragment.mAdapterTwo.setVisibles(true);
					JxccsBaseFragment.mAdapterThree.setVisibles(true);
					JxccsBaseFragment.mAdapterFour.setVisibles(true);
					JxccsBaseFragment.mAdapterFive.setVisibles(true);
					JxccsBaseFragment.mAdapterOne.notifyDataSetChanged();
					JxccsBaseFragment.mAdapterTwo.notifyDataSetChanged();
					JxccsBaseFragment.mAdapterThree.notifyDataSetChanged();
					JxccsBaseFragment.mAdapterFour.notifyDataSetChanged();
					JxccsBaseFragment.mAdapterFive.notifyDataSetChanged();
					return;
				}
				popWindow.dismiss();
			}
		});
		// 监听点击事件，点击其他位置，popupwindow小窗口消失
		view.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (popWindow != null && popWindow.isShowing()) {
					popWindow.dismiss();
					popWindow = null;
				}
				return true;
			}
		});
	}

	@Override
	public void btn_clearall(View view) {
		btn_clearall();
	}

	@Override
	protected void submit() {
		if (count == 0) {
			MyToast.getToast(this, "请至少选择一注").show();
		} else if (App.totalCount > 10000) {
			MyToast.getToast(this, "单注金额不能超过20000").show();
		} else {
			if (fragment != null) {
				fragment.submit();
				btn_clearall();
			}
		}
	}

	private void setFragmentItem(JxccsBaseFragment fragment) {
		this.fragment = fragment;
		FragmentTransaction transaction = getSupportFragmentManager()
				.beginTransaction();
		transaction.replace(containerID, fragment, "item")
				.commitAllowingStateLoss();
	}

	@Override
	public void onFragmentInteraction(int count) {
		this.count = count;
		setCountAndMoney(count);
	}

	private int getIndexByTypeId(int TypeId) {
		int index = 0;
		for (int i = 0; i < typeIDs.length; i++) {
			if (typeIDs[i] == TypeId) {
				index = i;
			}
		}
		return index;
	}

	@Override
	protected void onResume() {
		super.onResume();
		boolean isFromPaySuccess = getIntent().getBooleanExtra(
				"isFromPaySuccess", false);
		if (isFromPaySuccess) {
			int intExtra = getIntent().getIntExtra("JXSSCType", 0);
			Log.i(TAG, intExtra + "");
			int typeId = getIndexByTypeId(intExtra);
			mItemIndex = typeId;
			changePlayType(0, typeId);
			setTypeName(typeNames[typeId]);
		}
	}

	public void onShakeClick(View view) {
		fragment.onShakeClick();
	}

}
